
import Music, {musicName} from "../PublicScript/Music";
import ControlScript from "../PublicScript/ControlScript";
import GameData_red_blue_man, {dataName} from "./GameData_red_blue_man";
import CoinAnim from "./CoinAnim";
import GetSkin from "./GetSkin";
import PowerControl from "../PublicScript/PowerControl";
import FlyPower from "./FlyPower";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Singin extends cc.Component {

    @property({tooltip:"界面上的按钮", type: cc.Node})
    pageShow: cc.Node = null;

    @property(cc.SpriteFrame)
    frameSprite: cc.SpriteFrame[] = [];

    @property(cc.Node)
    point: cc.Node = null;

    @property({tooltip: "双倍领取", type:cc.Node})
    doubleBtn: cc.Node = null;
    @property({tooltip: "领取", type:cc.Node})
    getBtn: cc.Node = null;

    @property(cc.Node)
    finger: cc.Node = null;

    singInDay = 0;

    singInData = {};

    gold = 0;

    //获取签到奖励数据
    singInAward = null;

    onLoad () {
        this.init();
    }

    start () {

    }

    year = 0;
    month = 0;
    day = 0;
    init() {
        let date = new Date();
        this.year = date.getFullYear();
        this.month = date.getMonth();
        this.day = date.getDate();
        this.singInDay = 0;
        let data = ControlScript.loadData(dataName.singIn);
        if (data != null && data != undefined && data != "") {
            this.singInDay = parseInt(JSON.parse(data).singInDay);
            this.singInData = JSON.parse(data);
        }else{
            this.singInData["singInDate"] = [];
        }
        if(this.singInDay >= 7 && (this.singInData["singInDate"][0] != this.year || this.singInData["singInDate"][1] != this.month || this.singInData["singInDate"][2] != this.day)){
            this.singInDay = 0;
        }

        for(let i = 0; i <= this.singInDay; i++){
            if(i == this.singInDay){
                if(this.singInData["singInDate"][0] != this.year || this.singInData["singInDate"][1] != this.month || this.singInData["singInDate"][2] != this.day){
                    this.pageShow.children[this.singInDay].children[0].getComponent(cc.Sprite).spriteFrame = this.frameSprite[1];
                }
            }else{
                this.pageShow.children[i].getChildByName("state").active = true;
                this.pageShow.children[i].children[0].getComponent(cc.Sprite).spriteFrame = this.frameSprite[2];
            }
        }
        ControlScript.loadJson("Data/singin",(res)=>{
            this.singInAward = res.json;
        });

        if(GameData_red_blue_man.skinData[4].unLock == 0 && this.singInDay >= 6){
            this.doubleBtn.active = false;
            this.getBtn.x = 0;
        }
        this.pageShow.children[6].children[1].active = GameData_red_blue_man.skinData[4].unLock == 0;
        this.pageShow.children[6].children[2].active = !(GameData_red_blue_man.skinData[4].unLock == 0);
        if(this.singInDay >= 7 || (this.singInData["singInDate"][0] == this.year && this.singInData["singInDate"][1] == this.month && this.singInData["singInDate"][2] == this.day)){
            this.finger.active = false;
        }
    }


    onClick(e, d){
        Music.GamePlayEffect(musicName.click);
        if(this.singInDay >= 7){
            ControlScript.createToast("已经领取完全部奖励！");
            return;
        }

        if(this.singInData["singInDate"][0] == this.year && this.singInData["singInDate"][1] == this.month && this.singInData["singInDate"][2] == this.day){
            ControlScript.createToast("今天已经签到过了！");
            return;
        }

        this.finger.active = false;
        if(parseInt(d) == 1){
            GameData_red_blue_man.showVideo({callback:()=>{
                    this.getAward(2,this.year,this.month,this.day);
                }, name: "双倍领取签到奖励"});
        }else{
            this.getAward(1,this.year,this.month,this.day);
        }
    }

    //签到
    getAward(mul,year,month,day){
        let fun = ()=>{
            let index = this.singInDay;
            if(this.singInDay == 6 && GameData_red_blue_man.skinData[4].unLock == [0]){
                ControlScript.instantiate("getSkin", (res)=>{
                    res.getComponent(GetSkin).setInitData(5, 1);
                    GameData_red_blue_man.skinData[4].unLock = 1;
                    GameData_red_blue_man.saveSkinData();
                })
            }else{
                if(this.singInDay % 2 != 0 && this.singInDay != 0){
                    let num = this.singInAward[this.singInDay].power * mul;
                    PowerControl.addPower(num);

                    let pos2 =cc.find("Canvas/power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                    let pos1 = this.pageShow.children[this.singInDay].convertToWorldSpaceAR(cc.v2(0,0));
                    ControlScript.instantiate("flyPower",(res)=>{
                        res.getComponent(FlyPower).init(pos1, pos2, ()=>{

                        }, true)
                    })
                }else{
                    console.log("单数天！！！，加金币");
                    let gold = this.singInAward[this.singInDay].gold * mul;
                    let self = this;
                    cc.loader.loadRes("Prefab/coinAnim", (err,res)=>{
                        let view = cc.instantiate(res);
                        console.log(index, this.pageShow.children[index].x);
                        view.x = self.pageShow.children[index].x;
                        view.y = self.pageShow.children[index].y - 50;
                        view.parent = self.node;
                        view.getComponent(CoinAnim).createCoin();
                        view.getComponent(CoinAnim).setPoint(this.point, gold, true);
                    })
                }
            }

            let count = this.singInDay += 1;
            this.singInData["singInDay"] = count;
            this.singInData["singInDate"] = [year, month, day];
            ControlScript.saveData(dataName.singIn, this.singInData);
        };
        this.anim(fun);
    }

    //表现效果
    anim(callBack){
        let state = this.pageShow.children[this.singInDay].getChildByName("state");
        this.pageShow.children[this.singInDay].children[0].getComponent(cc.Sprite).spriteFrame = this.frameSprite[2];
        state.active = true;
        cc.tween(state)
            .to(0, {scale: 2})
            .to(0.1, {scale: 1})
            .call(()=>{
                callBack();
            })
            .delay(1)
            // .call(()=>{
            //     this.node.destroy();
            // })
            .start();
    }
    // update (dt) {}
}

